package cc.laop.business.system;

import cc.laop.business.BaseBusinessImpl;
import cc.laop.dao.system.ResourceMapper;
import cc.laop.dao.system.RoleMapper;
import cc.laop.entity.system.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;

@Component
public class ResourceBusiness
        extends BaseBusinessImpl<ResourceMapper, Resource> {

    @Autowired
    private RoleMapper roleMapper;

    @Override
    public int insert(Resource obj) {
        if (obj != null) {
            super.insert(obj);
            Map<String, Integer> map = new HashMap<String, Integer>();
            map.put("roleid", 1);
            map.put("resourceid", obj.getId());
            roleMapper.insertRoleResource(map);
            return obj.getId();
        }
        return 0;
    }

    public int insertOrUpdate(Resource res) {
        int result = 0;
        if (res != null) {
            if (res.getId() > 0) {
                result = super.update(res);
            } else {
                result = insert(res);
            }
        }
        return result;
    }

    /**
     * 删除菜单，包括所有子菜单及角色权限关联
     *
     * @param res
     */
    public void delete(Resource res) {
        if (res != null && res.getId() > 0) {
            List<Resource> list = new ArrayList<Resource>();
            list = selectAllSon(list, res.getId());
            list.add(res);
            for (Resource resource : list) {
                mapper.deleteResourceRole(resource.getId());
                super.delete(resource.getId());
            }
        }
    }

    /**
     * 获取所有子菜单
     *
     * @param list
     * @param id
     * @return
     */
    private List<Resource> selectAllSon(List<Resource> list, int id) {
        Resource res = new Resource();
        res.setParentid(id);
        List<Resource> reslist = super.select(res);
        if (reslist != null && reslist.size() > 0) {
            list.addAll(reslist);
            for (Resource resource : reslist) {
                selectAllSon(list, resource.getId());
            }
        }
        return list;
    }

    public List<Resource> toTree(List<Resource> rslist) {
        Map<Integer, Resource> map = new HashMap<Integer, Resource>();
        for (Resource resource : rslist) {
            map.put(resource.getId(), resource);
        }
        List<Resource> resourcelist = new ArrayList<Resource>(map.values());
        Collections.sort(resourcelist);
        List<Resource> newList = new ArrayList<Resource>();
        for (Resource res : resourcelist) {
            for (Resource sonRes : resourcelist) {
                if (res.getId() == sonRes.getParentid()) {
                    if (res.getResourceList() == null) {
                        List<Resource> sonList = new ArrayList<Resource>();
                        res.setResourceList(sonList);
                    }
                    res.getResourceList().add(sonRes);
                }
            }
            if (res.getParentid() == 0) {
                newList.add(res);
            }
        }
        return newList;
    }

}
